#include "GameObjects.h"
#include <iostream>

namespace binge
{

GameObjects::GameObjects(void)
{
    guid = 0;
}

GameObjects::~GameObjects(void)
{
    objectsId.clear();
    objectsLabel.clear();
    objectsAlive.clear();
    objectsLifetime.clear();
    objectsTimer.clear();
}

int // Returns Property Id
GameObjects::CreateObject(std::string label)
{
    int id = guid;
    Timer timer;

    objectsId.push_back(id);
    objectsLabel.push_back(label);
    objectsAlive.push_back(true);
    objectsLifetime.push_back(0);
    objectsTimer.push_back(timer);
    ++guid;

    return id;
}

void
GameObjects::RemoveObject(int id)
{
    int index = GetIndex(id);
    objectsId.erase(objectsId.begin()+index);
    objectsLabel.erase(objectsLabel.begin()+index);
    objectsAlive.erase(objectsAlive.begin()+index);
    objectsLifetime.erase(objectsLifetime.begin()+index);
    objectsTimer.erase(objectsTimer.begin()+index);
}

int
GameObjects::GetCount(void)
{
    return objectsId.size();
}

int
GameObjects::GetId(std::string label)
{
    int index = 0;
    std::vector<std::string>::iterator it;
    for(it = objectsLabel.begin(); it != objectsLabel.end(); it++)
    {
        if(*it == label)
        {
            return objectsId[index];
        }
        index++;
    }
    return -1; // Return -1 if Id doesn't exist
}

int
GameObjects::GetIndex(int id)
{
    int index = 0;
    std::vector<int>::iterator it;
    for(it = objectsId.begin(); it != objectsId.end(); it++)
    {
        if(*it == id)
        {
            return index;
        }
        index++;
    }
    return -1; // Return -1 if Id doesn't exist
}

int
GameObjects::GetIdByIndex(int index)
{
    return objectsId[index];
}

void
GameObjects::SetLabel(int id, std::string label)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        objectsLabel[index] = label;
    }
}

std::string
GameObjects::GetLabel(int id)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        return objectsLabel[index];
    }

    return ""; // Return empty string if Id doesn't exist
}

void
GameObjects::SetAlive(int id, bool value)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        objectsAlive[index] = value;
    }
}

bool
GameObjects::IsAlive(int id)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        return objectsAlive[index];
    }
    return false;
}

void
GameObjects::SetLifetime(int id, unsigned int value)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        objectsLifetime[index] = value;
    }
}

unsigned int
GameObjects::GetLifetime(int id)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        return objectsLifetime[index];
    }
    return 0;
}

bool
GameObjects::IsExpired(int id)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        if (objectsLifetime[index] > 0)
        {
            return objectsTimer[index].Stopwatch(objectsLifetime[index]);
        }
    }
    return false;
}

} // namespace
